package com.ilovepostcard;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.ilovepostcard.controller.OffLineManager;
import com.ilovepostcard.controller.PostcardController;
import com.ilovepostcard.data.OffLinePostcard;
import com.ilovepostcard.data.Postcard;
import com.ilovepostcard.utils.Constants;
import com.ilovepostcard.view.MyEditText;
import com.mobclick.android.MobclickAgent;

public class EditTextActivity extends BaseActivity implements OnClickListener{
	
	Button skip;
	ImageView mReturn;
	Button confrim;
	MyEditText mComment;
	//TextView mCounter;
	@Override
    public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edittext);
		Handler handler = new Handler();
		handler.post(new Runnable()
		{

			@Override
			public void run() {
				init();
				int page = OffLineManager.getInstance().getResumePage(EditTextActivity.this);
				if(page>2)
				{
					Intent nextPage = new Intent(EditTextActivity.this,PreviewActivity.class);
					EditTextActivity.this.startActivity(nextPage);
				}
				else
				{
					OffLineManager.getInstance().clearResumeInfo(EditTextActivity.this);
				}
			}
			
		});
		
		MobclickAgent.onEvent(this, Constants.COUNT_CREAT_TEXT);
		
	}
	
	protected void onResume()
	{
		super.onResume();
		MobclickAgent.onResume(this); 
	}
	
	protected void onPause()
	{
		super.onPause();
		MobclickAgent.onPause(this);
	}
	
	private void init()
	{
		skip = (Button)findViewById(R.id.pc_preview_skip);
		skip.setOnClickListener(this);
		
		mReturn = (ImageView)findViewById(R.id.title_return);
		mReturn.setOnClickListener(this);
		
		confrim = (Button)findViewById(R.id.pc_preview_confirm);
		confrim.setOnClickListener(this);
		
		
		mComment =  (MyEditText)findViewById(R.id.pc_preview_input);
		Postcard pc = PostcardController.getInstance().getCurrentPostCard();
		mComment.setText(pc.comment);
		
		//mCounter = (TextView)findViewById(R.id.Counter);
		//initEditText(pc.comment);
		/*mComment.addTextChangedListener(new TextWatcher() {   
            @Override  
            public void onTextChanged(CharSequence s, int start, int before, int count) {   

            	initEditText(s== null ? null : s.toString());
				
            }   
            @Override  
            public void beforeTextChanged(CharSequence s, int start, int count,   
                    int after) {   

            }   
            @Override  
            public void afterTextChanged(Editable s) {   
            	
            
            }   
        });  */

		if(pc.comment!= null)
			mComment.setSelection(pc.comment.length());
		
	}
	
	private void initEditText(String s)
	{
		int total = s == null ? 0 : s.length();
    	String message = EditTextActivity.this.getResources().getString(R.string.edit_counter);
		String formatMSG = String.format(message, total);
		//mCounter.setText(formatMSG);

		//mCounter.setTextColor(total>=140 ? Color.RED : EditTextActivity.this.getResources().getColor(R.color.color_gray_2));
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.pc_preview_skip)
		{
			if(OffLineManager.getInstance().getCurrentBackupPC()!= null)
				OffLineManager.getInstance().updateOffLinePc(this, OffLineManager.getInstance().getCurrentBackupPC().getName(), 2);
			Intent nextPage = new Intent(EditTextActivity.this,PreviewActivity.class);
			EditTextActivity.this.startActivity(nextPage);
		}
		else if(v.getId() == R.id.title_return)
		{
			finish();
		}
		else if(v.getId() == R.id.pc_preview_confirm)
		{
			//if(mComment.getText().toString()!= null && mComment.getText().toString().length()>0)
			{
				String comment = mComment.getText().toString();
				Postcard pc = PostcardController.getInstance().getCurrentPostCard();
				pc.comment = comment;
				OffLineManager.getInstance().getCurrentBackupPC().save(OffLinePostcard.TAG_COMMENT, comment);
				
			}
			if(OffLineManager.getInstance().getCurrentBackupPC()!= null)
				OffLineManager.getInstance().updateOffLinePc(this, OffLineManager.getInstance().getCurrentBackupPC().getName(), 2);
			Intent nextPage = new Intent(EditTextActivity.this,PreviewActivity.class);
			EditTextActivity.this.startActivity(nextPage);
		}
		
	}
	
	public void onDestroy()
	{
		super.onDestroy();
		
	}

}
